package cn.hicard.risk.dao.bd.impl;

import org.springframework.stereotype.Repository;

import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.core.Where;
import cn.hicard.risk.dao.BaseDao;
import cn.hicard.risk.dao.bd.JzgRiskestimateDao;
import cn.hicard.risk.entity.JzgRiskestimate;

@Repository
public class JzgRiskestimateDaoImpl extends BaseDao<JzgRiskestimate> implements JzgRiskestimateDao{

	@Override
	public void saveEstimate(JzgRiskestimate estimate) throws Exception {
		super.save(estimate);
	}
	
	@Override
	public void modifyEstimate(JzgRiskestimate estimate) throws Exception {
		super.saveOrUpdate(estimate);		
	}

	@Override
	public JzgRiskestimate findEstimate(int estimateId) throws Exception {
		return super.getByPk(estimateId);
	}

	@Override
	public PageResult<JzgRiskestimate> searchEstimate(
			JzgRiskestimate estimateStart, 
			JzgRiskestimate estimateEnd, 
			Page page) throws Exception{		
						
		String hql = "FROM JzgRiskestimate WHERE 1=1 ";
		String hqlCount = "SELECT COUNT(*) FROM JzgRiskestimate  WHERE 1=1 ";
				
		Where where = new Where(JzgRiskestimate.class);
		
		// 权限用户的条件
		where.addEqual("addloginid", estimateStart.getAddloginid());		
		// 商户名称
		where.addLike("merchname", estimateStart.getMerchname());		
		// 汇卡商户号
		where.addEqual("wpmerchno", estimateStart.getWpmerchno());		
		// 风险等级报表的条件
		where.addEqual("risklevel", estimateStart.getRisklevel());
		// 添加时间
		where.addBetween("adddatetime", estimateStart.getAdddatetime(), estimateEnd.getAdddatetime());
		
		// 暂时不栓查字段
		where.setMode(Where.MODE_NOT_CHECK);
		
		String start = estimateStart.getPredictdebitmoney();
		String end = estimateEnd.getPredictdebitmoney();
		
		// 预计月借记卡刷卡交易额报表的条件
		if(start != null && start.length()>0 && end!=null && end.length()>0)
			where.addBetween("predictdebitmoney + 0.0", Double.valueOf(start), Double.valueOf(end));			
		
		// 预计月贷记卡刷卡交易额报表的条件	
		start = estimateStart.getPredictcreditmoney();
		end = estimateEnd.getPredictcreditmoney();
		if(start != null && start.length()>0 && end!=null && end.length()>0)
			where.addBetween("predictcreditmoney + 0.0", Double.valueOf(start), Double.valueOf(end));
				
		// 预计每张签购单平均交易额报表的条件	
		start=estimateStart.getPredictsignorderavg();
		end = estimateEnd.getPredictsignorderavg();
		if(start != null && start.length()>0 && end!=null && end.length()>0)
			where.addBetween("predictsignorderavg + 0.0", Double.valueOf(start), Double.valueOf(end));
				
		// 预计每笔最高交易额报表的条件
		start = estimateStart.getPredictonemaxmoney();
		end = estimateEnd.getPredictonemaxmoney();
		if(start != null && start.length()>0 && end!=null && end.length()>0)
			where.addBetween("predictonemaxmoney + 0.0", Double.valueOf(start), Double.valueOf(end));
		
		// 预计每日最高交易额报表的条件
		start = estimateStart.getPredictdaymaxmoney();
		end = estimateEnd.getPredictdaymaxmoney();
		if(start != null && start.length()>0 && end!=null && end.length()>0)
			where.addBetween("predictdaymaxmoney + 0.0", Double.valueOf(start), Double.valueOf(end));
		
		// 愎复
		where.setMode(Where.MODE_CHECK);
		
		hql += where.queryString();
		hqlCount += where.queryString();
		
		PageResult<JzgRiskestimate> result = new PageResult<JzgRiskestimate>();
		result.setDataList(super.findByHqlPage(hql, page, where.getParams()));
		result.setResultCount(super.findCountByHql(hqlCount, where.getParams()));				
		return result;
	}


}
